package com.renchunlin.camerademo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private ImageView iv;
    private static final int REQ_1 = 1;
    private static final int REQ_2 = 2;
    private String filePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        iv = findViewById(R.id.iv);
        //获得系统路径
        filePath = Environment.getExternalStorageDirectory().getPath();
        //拼接上文件名
        filePath = filePath + "/" + "temp.png";
    }

    public void startCamera1(View view) {
        //通过隐士Intent的方式启动Camera
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, REQ_1);
    }

    public void startCamera2(View view) {
        //通过隐士Intent的方式启动Camera
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        Uri photoUri = null;
        File fileUrl = new File(filePath);

        ////API>=24 android 7.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            if (intent.resolveActivity(getPackageManager()) != null) {
                String imageName = fileUrl.getName();
                //7.0以上 的拍照文件必须在storage/emulated/0/Android/data/包名/files/pictures文件夹
                File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
                File file = new File(storageDir + "/" + imageName);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
                photoUri = FileProvider.getUriForFile(this, "com.renchunlin.camerademo.fileprovider", file);
            }
        } else {//API<24
            photoUri = Uri.fromFile(fileUrl);
        }

        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
        startActivityForResult(intent, REQ_2);
    }

    //自定义相机
    public void customCamera(View view) {
        startActivity(new Intent(this,CustomCamera.class));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == REQ_1) {
                //封装着所有传递回来的数据-整个图片的二进制流
                Bundle bundle = data.getExtras();
                Bitmap bitmap = (Bitmap) bundle.get("data");
                iv.setImageBitmap(bitmap);
            } else if (requestCode == REQ_2) {
                FileInputStream fileInputStream = null;
                try {
                    fileInputStream = new FileInputStream(filePath);
                    Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream);
                    iv.setImageBitmap(bitmap);
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
